<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml">

<head>

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">


    <title> - 登录</title>
    <meta name="keywords" content="">
    <meta name="description" content="">

    <link rel="shortcut icon" href="favicon.ico"> <link href="css/bootstrap.min.css?v=3.3.6" rel="stylesheet">
    <link href="css/font-awesome.css?v=4.4.0" rel="stylesheet">

    <link href="css/animate.css" rel="stylesheet">
    <link href="css/style.css?v=4.1.0" rel="stylesheet">
    <!--[if lt IE 9]>
    <meta http-equiv="refresh" content="0;ie.html" />
    <![endif]-->
    <script>if(window.top !== window.self){ window.top.location = window.location;}</script>
    <script src="js/jquery.min.js?v=2.1.4"></script>
    <script src="js/jsencrypt.min.js" rel="javascript"></script>
</head>

<body class="gray-bg">

    <div class="middle-box text-center loginscreen  animated fadeInDown">
        <div>
            <div>

                <h1 class="logo-name" style="font-size: 80px;">沃涵担保</h1>

            </div>
            <h3>欢迎使用 沃涵智慧云</h3>

            <form class="m-t" role="form">
                <div class="form-group">
                    <input type="text" id="name" name="name" class="form-control" placeholder="用户名" value="admin" required="">
                </div>
                <div class="form-group">
                    <input type="password" id="pwd" name="pwd" class="form-control" placeholder="密码" value="Zpepc001@" required="">
                </div>
                <div class="login-button">
                    <button type="submit" class="btn btn-primary block full-width m-b">登 录</button>
                </div>
            </form>
        </div>
    </div>

    <!-- 全局js -->
    <script src="js/jquery.min.js?v=2.1.4"></script>
    <script src="js/bootstrap.min.js?v=3.3.6"></script>

    <script type="text/javascript">
        var redirect_url = '';
        let surveyConfig = {};
        $(function () {
            $('#password').bind('keyup', function(event) {
                if (event.keyCode == "13") {
                    //回车执行查询
                    $('.login-button').click();
                }
            });

            $('.login-button').click(function (e) {
                e.preventDefault();
                let name = $("#name").val();
                let pwd = $("#pwd").val();
                pwd = encryptPwd(pwd);
                console.log(pwd)
                const url = '/sso/doLogin';
                $.ajax({
                    url: url,
                    type: 'POST',
                    data: {
                        name: name,
                        pwd: pwd
                    },
                    beforeSend: function (xhr) {

                    },
                    success: function (resp) {
                        sessionStorage.setItem("access_token", resp.data.tokenValue);
                        if (redirect_url != '') {
                            window.location.href = redirect_url;
                        } else {
                            window.location.href = "/"
                        }
                    },
                    error: function (resp) {
                        if(resp.status == 401) {
                            if(resp.responseJSON && resp.responseJSON.message) {
                                $("#error_msg").text(resp.responseJSON.message);
                            } else {
                                $("#error_msg").text("用户名密码或验证码不匹配");
                            }
                        }

                        $(".error_message").show();
                    }
                })
            })

            redirect_url = getQueryVariable('redirect_url');
        });

        function hasClass(elem, cls) {
            cls = cls || '';
            if (cls.replace(/\s/g, '').length == 0) return false; //当cls没有参数时，返回false
            return new RegExp(' ' + cls + ' ').test(' ' + elem.className + ' ');
        }

        function addClass(ele, cls) {
            if (!hasClass(ele, cls)) {
                ele.className = ele.className == '' ? cls : ele.className + ' ' + cls;
            }
        }
        const PUBLIC_KEY = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAvrvh/BoZ6bcnCltEWSyT9WnDOlfwXOHWotOS3j1rgE50UjQ82CFKArvVj5qpTT2AJAlDYz0EzTDNXKluyPcOzxL7qJSdASjJuR5ewrXJcL/6Q51465vQqijlkYrZx/kg57Oy5eVSJKZhTpNiwAM0iJ7Mjt7gzPU4nqQCkzb8ZLvl8O/+93hrMQu4zk+QWjeBz6rqWNPZA4prOedUUUQIlsUpClL8+HuF6lhc4GiyXKIuqFv7yfKECZfdfI8+e5Awxc57sRqyTOMtQD34P5t6anYQhyTuM5CVHW/Z8q7o0MDeJ941MxmxUxlCASFbZPAfqOMBNI5UjjKs5uiB0CIvXQIDAQAB";
        function encryptPwd(txt) {
            const encryptor = new JSEncrypt();
            encryptor.setPublicKey(PUBLIC_KEY);
            return encryptor.encrypt(txt);
        }

        function removeClass(ele, cls) {
            if (hasClass(ele, cls)) {
                var newClass = ' ' + ele.className.replace(/[\t\r\n]/g, '') + ' ';
                while (newClass.indexOf(' ' + cls + ' ') >= 0) {
                    newClass = newClass.replace(' ' + cls + ' ', ' ');
                }
                ele.className = newClass.replace(/^\s+|\s+$/g, '');
            }
        }

        // document.querySelector("#code").onclick = function () {
        //     $.ajax({
        //         url: "/auth/session/code",
        //         method: 'GET',
        //         success: function(resp) {
        //             let data = resp.data;
        //             let captchaBase64 = data.captchaBase64;
        //             window.key = data.key;
        //             $("#code").attr("src", captchaBase64);
        //         }
        //     })
        // }
        document.querySelector(".register").onclick = function(){
            window.location.href = "/viewer/visitorRegister.html"
        }

        document.querySelector(".login-button").onclick = function () {
            $(".error_message").hide();
            const username = document.querySelector('#username').value;
            let password = document.querySelector('#password').value;
            const imageCode = document.querySelector("#imageCode").value;
            if(!imageCode && !surveyConfig.singleModel) {
                $("#error_msg").text("验证码不能为空");
                $(".error_message").show();
            } else {
                $(".error_message").hide();
            }
            password = encryptPwd(password);
            var url = '/auth/oauth/token?grant_type=password&username=' + username + '&password=' + password;
            console.log(url);
            $.ajax({
                url: url,
                type: 'POST',
                beforeSend: function (xhr) {
                    xhr.setRequestHeader("Authorization", "Basic YW5kcm9pZDphbmRyb2lk");
                },
                success: function (resp) {
                    sessionStorage.setItem("access_token", resp['access_token']);
                    sessionStorage.setItem("refresh_token", resp['refresh_token']);
                    if (redirect_url != '') {
                        // redirect_url += '?access_token='+ resp['access_token'];
                        window.location.href = redirect_url;
                    } else {
                        window.location.href = "/"
                    }
                },
                error: function (resp) {
                    if(resp.status == 401) {
                        if(resp.responseJSON && resp.responseJSON.message) {
                            $("#error_msg").text(resp.responseJSON.message);
                        } else {
                            $("#error_msg").text("用户名密码或验证码不匹配");
                        }
                    }

                    $(".error_message").show();
                }
            })
        }

        function getQueryVariable(variable) {
            var query = window.location.search.substring(1);
            var vars = query.split("&");
            for (var i = 0; i < vars.length; i++) {
                var pair = vars[i].split("=");
                if (pair[0] == variable) {
                    return pair[1];
                }
            }
            return (false);
        }

        function enterLogin(e) {
            var theEvent = e || window.event;
            var code = theEvent.keyCode || theEvent.which || theEvent.charCode;
            if (code == 13) {
                $('.login-button').click();
                return false;
            }
            return true;
        }
    </script>
    

</body>

</html>
